﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.IO;
using System.Threading;
using System.Xml.Linq;

namespace SlvPartecipazione
{
    public partial class Invito : Page
    {
        private int _zIndex = 10;
 
        public Invito()
        {
            InitializeComponent();
            
            this.LayoutRoot.SizeChanged += new SizeChangedEventHandler(LayoutRoot_SizeChanged);

            txbSalva.MouseLeftButtonUp += new MouseButtonEventHandler(txbSalva_MouseLeftButtonUp);
        }

        public Invito(string GUID)
        {
            InitializeComponent();

            getInvitato(GUID);

            this.LayoutRoot.SizeChanged += new SizeChangedEventHandler(LayoutRoot_SizeChanged);

            txbSalva.MouseLeftButtonUp += new MouseButtonEventHandler(txbSalva_MouseLeftButtonUp);
        }

        public void LoadMp3()
        {
            sottofondo.Source = new Uri("http://www.sanitelitalia.it/TEST/jo.mp3");
        }

        public void getInvitato(string guid)
        {
            //XDocument xmlDoc = XDocument.Load(@"ListaPartecipanti.xml");
            var q = from c in MainPage.XDoc.Descendants("PARTECIPANTE")
                    where c.Element("GUID").Value == guid
                    select new
                    {
                        titolo = c.Element("TITOLO").Value,
                        nome = c.Element("NOME").Value,
                        cognome = c.Element("ACCOMPAGNATORE").Value
                    };

            foreach (var obj in q)
            {
                txbInvitato.Text = obj.titolo + " " + obj.nome;
                if (!String.IsNullOrEmpty(obj.cognome))
                {
                    txbInvitato.Text = txbInvitato.Text + " e " + obj.cognome;
                }
            }
            
        }

        private void FlipImage(object sender, MouseButtonEventArgs e)
        {
            Image image = sender as Image;

            image.SetValue(Canvas.ZIndexProperty, _zIndex++);

            if (image.Name.Equals("invitoChiesa"))
            {
                txbSalva.Visibility = System.Windows.Visibility.Visible;
            }

            Storyboard flip = new Storyboard();

            var widthAnimation = new DoubleAnimationUsingKeyFrames();

            var heightAnimation = new DoubleAnimationUsingKeyFrames();

            DoubleAnimation animation = new DoubleAnimation()
            {
                Duration = new TimeSpan(0, 0, 0, 0, 150)
            };

            flip.Children.Add(animation);

            if (image.Projection == null)
            {
                image.Projection = new PlaneProjection();
            }

            PlaneProjection projection = image.Projection as PlaneProjection;

            if (projection.LocalOffsetX < 0)
            {
                animation.To = -50;
                Storyboard.SetTarget(animation, projection);
                Storyboard.SetTargetProperty(animation, new PropertyPath(PlaneProjection.LocalOffsetYProperty));

                flip.Completed += (_, a) =>
                {
                    flip = new Storyboard();

                    animation = new DoubleAnimation()
                    {
                        Duration = new TimeSpan(0, 0, 0, 0, 400)
                    };

                    flip.Children.Add(animation);

                    animation.To = 0;
                    Storyboard.SetTarget(animation, projection);
                    Storyboard.SetTargetProperty(animation, new PropertyPath(PlaneProjection.LocalOffsetXProperty));

                    flip.Completed += (__, aa) =>
                    {
                        flip = new Storyboard();

                        animation = new DoubleAnimation()
                        {
                            Duration = new TimeSpan(0, 0, 0, 0, 500)
                        };

                        flip.Children.Add(animation);

                        animation.To = 0;
                        Storyboard.SetTarget(animation, projection);
                        Storyboard.SetTargetProperty(animation, new PropertyPath(PlaneProjection.LocalOffsetYProperty));

                        flip.Begin();
                    };

                    flip.Begin();
                };

                flip.Begin();
            }
            else
            {

                animation.To = -50;
                Storyboard.SetTarget(animation, projection);
                Storyboard.SetTargetProperty(animation, new PropertyPath(PlaneProjection.LocalOffsetYProperty));

                flip.Completed += (_, a) =>
                {
                    flip = new Storyboard();

                    animation = new DoubleAnimation()
                    {
                        Duration = new TimeSpan(0, 0, 0, 0, 400)
                    };

                    flip.Children.Add(animation);

                    animation.To = -800;
                    Storyboard.SetTarget(animation, projection);
                    Storyboard.SetTargetProperty(animation, new PropertyPath(PlaneProjection.LocalOffsetXProperty));

                    flip.Completed += (__, aa) =>
                    {
                        flip = new Storyboard();

                        animation = new DoubleAnimation()
                        {
                            Duration = new TimeSpan(0, 0, 0, 0, 500)
                        };

                        flip.Children.Add(animation);

                        animation.To = 0;
                        Storyboard.SetTarget(animation, projection);
                        Storyboard.SetTargetProperty(animation, new PropertyPath(PlaneProjection.LocalOffsetYProperty));

                        flip.Begin();
                    };

                    flip.Begin();
                };

                flip.Begin();
            }
            
        }
 

        // Executes when the user navigates to this page.
        public void Start()
        {
            sottofondo.MediaOpened += new RoutedEventHandler(_Sound_MediaOpened);
            sottofondo.Play();

            sbCopertinaBW.Begin();
        }

        void _Sound_MediaOpened(object sender, RoutedEventArgs e)
        {
            MediaElement _Sound = sender as MediaElement;
            _Sound.Play();
        }

        void LayoutRoot_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Grid g = sender as Grid;

            double canvasheight = (g.RowDefinitions.Count > 0) ? g.RowDefinitions[0].ActualHeight : g.ActualHeight;
            double canvaswidth = g.ActualWidth;

            double scaleX = canvaswidth / Wall.Width;
            double scaleY = canvasheight / Wall.Height;

            if (scaleX > scaleY)
                scaleX = scaleY;
            else
                scaleY = scaleX;

            this.WallStretch.ScaleX = scaleX;
            this.WallStretch.ScaleY = scaleY;
            this.WallTranslate.X = (g.ActualWidth - Wall.Width * scaleX) / 2;
            this.UpdateLayout();
        }

        private void txbSalva_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Uri u = System.Windows.Browser.HtmlPage.Document.DocumentUri;
            string firstUri = u.ToString().Remove(u.ToString().IndexOf(u.AbsolutePath));
            System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(firstUri + "/invito.pdf", UriKind.Absolute), "blank");
        }

        private void copertinaBW_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            sbCopertinaBW.Stop();
            copertinaBW.Visibility = System.Windows.Visibility.Collapsed;

            FlipImage(copertina, e);
        }
       
       
    }
}
